package com.base.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.base.application.AppApplication;
import com.base.application.AppComponent;
import com.gml.gao.R;

/**
 * Created by gml on 17/1/6.
 */

public abstract class BaseActivity extends AppCompatActivity {


    private ToolBarHelper mToolBarHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initInjector();
        initView();
        initToolBar();
    }

    private void initToolBar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);


            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }

            mToolBarHelper = new ToolBarHelper(toolbar,actionBar);
            handleToolBar(mToolBarHelper);
        }
    }


    /**
     * 子类去实现
     *
     * @param toolBarHelper
     */
    protected void handleToolBar(ToolBarHelper toolBarHelper) {

    }


    public static class ToolBarHelper {

        private Toolbar mToolbar;

        private ActionBar mActionBar;

        public ToolBarHelper(Toolbar toolbar) {
            mToolbar = toolbar;
        }

        public ToolBarHelper(Toolbar toolbar, ActionBar actionBar) {
            mToolbar = toolbar;
            mActionBar = actionBar;
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }

        public Toolbar getToolbar() {
            return mToolbar;
        }

        /***
         * 设置左边返回图标是否可见
         *
         * 默认在 baseActivit 设置为可见状态
         */
        public void setHomeAsUpEnabled(boolean isShow) {
            mActionBar.setDisplayHomeAsUpEnabled(isShow);
        }
        /**
         * 设置标题
         * @param title
         */
        public void setTitle(String title) {
            mToolbar.setTitle(title);
        }

        /**
         * 设置logo
         * 默认不显示 logo
         */
        public void setLogo(){
            mToolbar.setLogo(R.mipmap.ic_launcher);
        }

        /**
         * 设置左侧返回键样式
         * @param img
         */
        public void setNavigationIcon(int img){
            mToolbar.setNavigationIcon(img);
        }




    }

    /**
     *  默认处理boolsbar 左面按钮事件 为返回操作 如有特色需求子类从写该方法
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }



    //  建议写在基类Activity里
    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    //  建议写在基类Activity里
    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(AppApplication.get(this).getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    private void initInject() {
        // 构建Component并注入
        getActivityComponent().inject(this);
    }


    /**
     * 获取 ApplicationComponent
     *
     * @return ApplicationComponent
     */
    protected AppComponent getAppComponent() {
        return AppApplication.get(this).getAppComponent();
//        return ((MvpApplication) getActivity().getApplication().get).getAppComponent();
    }

    /**
     * Dagger 注入
     */
    protected abstract void initInjector();


//    public abstract int getLayoutId();

    public abstract void initView();
}
